package gameSocketer
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.*;
	import flash.net.Socket;
	import flash.utils.ByteArray;

	public class Socketer
	{
		private var socket:Socket=new Socket();
		private var needReadHead:Boolean=true;//判断一个包是否读取完整，为ture完整，false不完整
		private var dataLength:uint=0;
		public var sendData:ByteArray = new ByteArray();
		public var revData:ByteArray = new ByteArray();
//		public static var TmpArray:ByteArray=new ByteArray();
		
		public function Socketer(host:String,port:int)
		{
			sendData.length=255;
			revData.length=255;
			socket.addEventListener(Event.CONNECT,OnConnect);
			socket.addEventListener(Event.CLOSE,OnClose);
			socket.addEventListener(ProgressEvent.SOCKET_DATA,OnSocketData);
			socket.addEventListener(IOErrorEvent.IO_ERROR,OnIoError);
			socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,OnSecurityError);
			socket.connect(host,port);
		}
		private function OnConnect(e:Event):void{
			trace("OnConnect");
		}
		private function OnClose(e:Event):void{
			trace("OnClose");
		}
		private function OnIoError(e:IOErrorEvent):void{
			trace("OnIoError");
		}
		private function OnSecurityError(e:SecurityErrorEvent):void{
			trace("OnSecurityError");
		}
		private function OnSocketData(e:ProgressEvent):void{
			trace("OnSocketData");
			while(socket.bytesAvailable){
				//读包长
				if(needReadHead){
					dataLength = socket.readShort();//读包长！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
					trace("收到的包长："+dataLength);
					needReadHead=false;
				}
				//读包体
				if(!needReadHead){
					if(socket.bytesAvailable >= dataLength){
						revData.position=0;
						socket.readBytes(revData,0,dataLength);
						revData.position=0;
						
						//测试++++++++++++++++++++++++++++++++++++++++++++++++++++++++
						if(revData.readByte()>0){
							revData.position=0;
							trace("收包 大类:"+revData.readByte()+" 小类:"+revData.readByte()+" 长度："+dataLength);
						}else{
							revData.position=0;
							trace("收包 大类:"+revData.readByte()+" 长度："+dataLength);
						}
						revData.position=0;
						//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
						
						parseData(dataLength);//分析包.......
						needReadHead=true;
					}else{
						trace("可能为未知包 包长:"+socket.bytesAvailable);
						break;
					}
				}
			}			
		}
		public function send():void
		{ 
			socket.writeShort(sendData.position);//包长！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
			socket.writeBytes(sendData,0,sendData.position);//包数据
			socket.flush();
			
			//测试++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			var dataLength:int=sendData.position;
			sendData.position=0;
			if(sendData.readByte()>0){
				sendData.position=0;
				trace("发包 大类:"+sendData.readByte()+" 小类:"+sendData.readByte()+" 长度："+dataLength);
			}else{
				sendData.position=0;
				trace("发包 大类:"+sendData.readByte()+" 长度："+dataLength);
			}
			//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			sendData.position=0;
		}
		
		//分析包
		public function parseData(length:int):void{
			switch(revData.readByte()){
				case 1:
					BlackJack.loginPaser.parseData(length);
					break;
				case 2:
					BlackJack.cardGameSocketPaser.parseData(length);
					break;
				case 4:
					BlackJack.chipSocketPaser.parseData(length);
					break;
			}
		}
	}
}